﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RZD_maps.Modules.Transport.ViewModels;
using RZD_maps.Infrastructure;
using System.Collections.ObjectModel;
using System.ComponentModel;


namespace RZD_maps.Modules.Transport.Models
{
    public class Model
    {
        public ViewModel ViewModel { get; set; }

        public struct dt
        {
            public int Id;
            public double Lat;
            public double Lng;
            public double Velocity;
        }

        public List<dt> list_data = new List<dt>();

        public void ChangeData(TrainPosition data)
        {
            list_data.Clear();
            foreach (var item in data.list)
            {
                dt dt = new dt();
                dt.Id = item.id;
                dt.Lat = item.lat;
                dt.Lng = item.lng;
                dt.Velocity = 0.0;
                list_data.Add(dt);
            }
        }
            private ObservableCollection<TransportCategory> categories;

    public ObservableCollection<TransportCategory> Categories
    {
      get { return categories; }
      set {
        categories = value;
        RaisePropertyChangedEvent("Categories");
      }
    }


    /// <summary>
    /// Refreshes the data.
    /// </summary>
    public void RefreshData()
    {
        Categories = TransportUtil.CreateTransport();//StationUtil.CreateShop();
    }


    ///<summary>
    ///Occurs when a property value changes.
    ///</summary>
    public event PropertyChangedEventHandler PropertyChanged;


    /// <summary>
    /// Fires the <see cref="PropertyChanged"/> event for a
    /// given property.
    /// </summary>
    /// <param name="propertyName">The changed property.</param>
    private void RaisePropertyChangedEvent(string propertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }


    public Model()
    {
      RefreshData();
    }

    public TransportCategory TryFindCategoryByName(string categoryName)
    {
      return TryFindCategoryByName(null, categoryName);
    }


    public TransportCategory TryFindCategoryByName(TransportCategory parent, string categoryName)
    {
      ObservableCollection<TransportCategory> cats;
      cats = parent == null ? categories : parent.SubCategories;
      foreach (TransportCategory category in cats)
      {
        if (category.CategoryName == categoryName)
        {
          return category;
        }
        else
        {
          TransportCategory cat = TryFindCategoryByName(category, categoryName);
          if (cat != null) return cat;
        }
      }

      return null;
    }


    }
}
